﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Comod_StorageAdd : System.Web.UI.Page
{
    Function f = new Function();
    ZSql zs = new ZSql();
    ZSql info = new ZSql();//添加修改
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblUser.Text = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);//操作人
            lblPatd.Text = f.getModuleidAdd(Request.QueryString["moduid"]);//当前位置
            lblTime.Text = DateTime.Now.ToShortDateString();
            storageTime.Text = DateTime.Now.ToString();
            hidModuid.Value = Request.QueryString["moduid"];
            ddlComoType();
            initWarehouse();
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))//修改操作
            {
                initData();
            }
            else
            {
                storageTime.Text = DateTime.Now.ToString();//初始化录入时间
                lblTime.Text = DateTime.Now.ToShortDateString();//初始化操作时间
            }
        }
    }
    private void initData()//当修改时,初使化数据
    {
        ZSql rs = new ZSql();
        if (!string.IsNullOrEmpty(Request.QueryString["id"]))
        {
            string sql = "select top 1 * from SV_Stock where StockId=" + Request.QueryString["id"].ToString();
            //Response.Write(sql);
           // Response.End();
            rs.Open(sql);
            if (rs.m_table.Rows.Count > 0)
            {

                ddlType.SelectedValue = rs.m_table.Rows[0]["TypeId"].ToString();
                catalogueName.Text = rs.m_table.Rows[0]["catalogueName"].ToString();//名称
                ComodBarcode.Text = rs.m_table.Rows[0]["ComodBarcode"].ToString();
                ProcNum.Value = rs.m_table.Rows[0]["ProcNum"].ToString();
                Units.Text = rs.m_table.Rows[0]["Units"].ToString();//单位
                Spec.Text = rs.m_table.Rows[0]["Spec"].ToString();//进库数量
                Remark.Text = rs.m_table.Rows[0]["Remark"].ToString();//备注
                if (rs.m_table.Rows[0]["AuditState"].ToString() == "1") AuditState.Checked = true; else AuditState1.Checked = true;
                storageTime.Text = rs.m_table.Rows[0]["storageTime"].ToString();//初始化录入时间
                lblTime.Text = rs.m_table.Rows[0]["EditDate"].ToString();//初始化操作时间
                ProcMoney.Text = rs.m_table.Rows[0]["ProcMoney"].ToString();
                SuppliMoney.Text = rs.m_table.Rows[0]["SuppliMoney"].ToString();
                Prices.Text = rs.m_table.Rows[0]["Prices"].ToString();
                serviceMoney.Text = rs.m_table.Rows[0]["serviceMoney"].ToString();
            }
        }
        rs.Close();
    }
    protected void initWarehouse()//库房
    {
        ZSql ware = new ZSql();
        ware.Open("select warehouseid,warehousename from SV_Warehouse");
        if (ware.m_table.Rows.Count > 0)
        {
            ddlWarehouse.DataSource = ware.m_table;
            ddlWarehouse.DataTextField = "warehousename";
            ddlWarehouse.DataValueField = "warehouseid";
            ddlWarehouse.DataBind();

            ware.Close();
        }
    }

    private void ddlComoType()//类型
    {

        ZSql com = new ZSql();
        com.Open("select TypeId,TypeName from SV_Type where AuditState=1");
        ddlType.DataSource = com.m_table;
        ddlType.DataTextField = "TypeName";
        ddlType.DataValueField = "TypeId";
        ddlType.DataBind();
        ddlType.Items.Insert(0, new ListItem("选择备份类型", ""));
        com.Close();

    }


    protected void btnAdd_Click(object sender, EventArgs e)//系统提交
    {

        ZSql info = new ZSql();
        string action = Request.QueryString["action"];
        string sql = "";
        if (action == "add")//新增
            sql = "select top 1 * from SV_Stock";
        else//修改
            sql = "select top 1 * from SV_Stock where StockId=" + Request.QueryString["id"];

        info.Open(sql);
        info.NextRow();
        if (action == "add")//新增
        {
            info.NewRow();
        }
        info["TypeId"] = ddlType.SelectedValue.ToString();//采购价
        info["ComodBarcode"] = ComodBarcode.Text.ToString();//条码
        info["catalogueName"] = catalogueName.Text.ToString();
        info["TypeId"] = ddlType.SelectedValue.ToString();//类型ID
        info["Units"] = Units.Text.ToString();//单位
        info["WarehouseId"] = ddlWarehouse.SelectedValue.ToString();//仓库
        info["Spec"] = Spec.Text.ToString();//
        info["ProcNum"] = ProcNum.Value.ToString();//数量
        info["Remark"] = Remark.Text;//备注
        info["storageTime"] = storageTime.Text.ToString();//入库时间
        info["AuditState"] = Request.Form["AuditState"];
        info["EditUserName"] = Server.UrlDecode(Request.Cookies["user"].Values["realname"]);//编辑人
        info["EditDate"] = DateTime.Now.ToString();//时间
        info["ProcMoney"] = ProcMoney.Text.ToString();
        info["SuppliMoney"] = SuppliMoney.Text.ToString();
        info["Prices"] = Prices.Text.ToString();
        info["serviceMoney"] = serviceMoney.Text.ToString();

        info.Update();
        info.Close();
        Response.Write("<script>alert('操作已成功，请返回！');</script>");//返回页面
        Response.Write("<script>window.location='StorageManage.aspx?moduid=" + hidModuid.Value + "';</script>");//返回页面


    }


    

   /* protected void ddlComodBarcode_SelectedIndexChanged(object sender, EventArgs e)
    {
        ZSql rs = new ZSql();
        if (!string.IsNullOrEmpty(ddlComodBarcode.Text))
        {
            string sql = "select top 1 *,(select Units from Units where SV_Catalogue.Units=UnitsId) as  UnitsName from SV_Catalogue where ComodBarcode='" + ddlComodBarcode.SelectedValue.ToString()+"'";
            rs.Open(sql);
            if (rs.m_table.Rows.Count > 0)
            {
                catalogueName.Text = rs.m_table.Rows[0]["catalogueName"].ToString();//名称
                Units.Text = rs.m_table.Rows[0]["UnitsName"].ToString();//单位
                Spec.Text = rs.m_table.Rows[0]["Spec"].ToString();//进库数量
                Remark.Text = rs.m_table.Rows[0]["Remark"].ToString();//备注
                if (rs.m_table.Rows[0]["AuditState"].ToString() == "1") AuditState.Checked = true; else AuditState1.Checked = true;
            }
        }
        rs.Close();
    }*/
}
